package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IYcMeasuresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

/**
 * 油厂措施管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-22
 */
@Service
public class YcMeasuresServiceImpl implements IYcMeasuresService {
    @Autowired
    private YcMeasuresMapper ycMeasuresMapper;

    @Autowired
    private YcIndeMapper ycIndeMapper;

    @Autowired
    private YcDataMapper ycDataMapper;

    @Autowired
    private YcOrgMapper ycOrgMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询油厂措施管理
     *
     * @param meaId 油厂措施管理主键
     * @return 油厂措施管理
     */
    @Override
    public YcMeasures selectYcMeasuresByMeaId(Long meaId) {
        return ycMeasuresMapper.selectYcMeasuresByMeaId(meaId);
    }

    /**
     * 查询油厂措施管理通过dataId
     *
     * @param dataId
     * @return 油厂措施管理
     */
    @Override
    public YcMeasures selectYcMeasuresByDataId(Long dataId) {
        return ycMeasuresMapper.selectYcMeasuresByDataId(dataId);
    }

    /**
     *
     * 查询油厂措施管理列表
     *
     * @param ycMeasures 油厂措施管理
     * @return 油厂措施管理
     */
    @Override
    public List<YcMeasures> selectYcMeasuresList(YcMeasures ycMeasures) {
        return ycMeasuresMapper.selectYcMeasuresList(ycMeasures);
    }

    /**
     *
     * 查询油厂措施管理列表 导出专用
     *
     * @param ycMeasures 油厂措施管理
     * @return 油厂措施管理
     */
    @Override
    public List<YcMeasures> selectYcMeasuresListUseExport(YcMeasures ycMeasures) {
        boolean district = false;
        if(sysDeptMapper.selectDeptById(ycMeasures.getDeptId()) != null) {
            SysDept sysDept = sysDeptMapper.selectDeptById(ycMeasures.getDeptId());
            List<YcOrg> districtOrgs = ycOrgMapper.selectYcOrgByLevel("1");
            for (YcOrg districtOrg : districtOrgs) {
                if (districtOrg.getOrgName().equals(sysDept.getDeptName())) {
                    district = true;
                }
            }
        }
        List<YcMeasures> measuresList = ycMeasuresMapper.selectYcMeasuresList(ycMeasures);
        if(district){
            List<YcMeasures> list1 = ycMeasuresMapper.selectDistrictYcMeasure1(ycMeasures);
            SysDept sysDept = sysDeptMapper.selectDeptById(ycMeasures.getDeptId());
            for (YcMeasures element:list1){
                if (ycDataMapper.selectYcDataByDataId(element.getDataId()) != null){
                    YcData ycData = ycDataMapper.selectYcDataByDataId(element.getDataId());
                    if(ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId()) != null){
                        YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId());
                        if(ycOrg.getOrgName().equals(sysDept.getDeptName())){
                            measuresList.add(element);
                        }
                    }
                }
            }
        }

        for (YcMeasures ycMeasure : measuresList){
            if(ycIndeMapper.selectYcIndeByIndeId(ycMeasure.getIndeId()) != null){
                YcInde ycInde = ycIndeMapper.selectYcIndeByIndeId(ycMeasure.getIndeId());
                if(sysDeptMapper.selectDeptById(ycInde.getDeptId()) != null){
                    SysDept sysDept1 = sysDeptMapper.selectDeptById(ycInde.getDeptId());
                    ycMeasure.setDeptName(sysDept1.getDeptName());
                }
                ycMeasure.setDimension( Integer.parseInt(String.valueOf(ycInde.getIndeDimension())));
                ycMeasure.setIndeName(ycInde.getIndeName());
            }
            if(ycDataMapper.selectYcDataByDataId(ycMeasure.getDataId()) != null){
                YcData ycData = ycDataMapper.selectYcDataByDataId(ycMeasure.getDataId());
                if(ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId()) != null){
                    YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId());
                    ycMeasure.setOrgName(ycOrg.getOrgName());
                }
                ycMeasure.setDataDimension(ycData.getDataDimension());
                ycMeasure.setDataYear(ycData.getDataYear());
            }

        }
        return measuresList;
    }

    /**
     * 查询更多油厂措施管理列表admin
     *
     * @param ycMeasures_v2 油厂措施管理
     * @return 油厂措施管理集合
     */
    public List<YcMeasures> selectYcMeasuresListMoreAdmin(YcMeasures_v2 ycMeasures_v2) {
        return ycMeasuresMapper.selectYcMeasuresListMore(ycMeasures_v2);
    }

    /**
     * 查询更多油厂措施管理列表
     *
     * @param ycMeasures_v2 油厂措施管理
     * @return 油厂措施管理集合
     */
    public List<YcMeasures> selectYcMeasuresListMore(YcMeasures_v2 ycMeasures_v2) {
        boolean district = false;
        List<YcMeasures> list = new ArrayList<>();
        SysDept sysDept = sysDeptMapper.selectDeptById(ycMeasures_v2.getDeptId());
        List<YcOrg> districtOrgs = ycOrgMapper.selectYcOrgByLevel("1");
        for (YcOrg districtOrg:districtOrgs){
            if(districtOrg.getOrgName().equals(sysDept.getDeptName())){
                district = true;
            }
        }

        if(district){
            list = ycMeasuresMapper.selectYcMeasuresListMore(ycMeasures_v2);
            List<YcMeasures> list1 = ycMeasuresMapper.selectDistrictYcMeasure(ycMeasures_v2);
            for (YcMeasures element:list1){
                if (ycDataMapper.selectYcDataByDataId(element.getDataId()) != null){
                    YcData ycData = ycDataMapper.selectYcDataByDataId(element.getDataId());
                    if(ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId()) != null){
                        YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId());
                        if(ycOrg.getOrgName().equals(sysDept.getDeptName())){
                            list.add(element);
                        }
                    }
                }
            }
        }else{
            list = ycMeasuresMapper.selectFactoryYcMeasuresListMore(ycMeasures_v2);
        }
        return list;
    }

    /**
     * 查询更多油厂措施管理列表(管理界面专用)
     *
     * @param ycMeasures_v2 油厂措施管理
     * @return 油厂措施管理集合
     */
    public List<YcMeasures> selectYcMeasuresListMore1(YcMeasures_v2 ycMeasures_v2) {
        boolean district = false;   //dept是五个作业区
        List<YcMeasures> list = new ArrayList<>();
        SysDept sysDept = sysDeptMapper.selectDeptById(ycMeasures_v2.getDeptId());
        List<YcOrg> districtOrgs = ycOrgMapper.selectYcOrgByLevel("1");  //找到所有区级组织
        for (YcOrg districtOrg:districtOrgs){
            if(districtOrg.getOrgName().equals(sysDept.getDeptName())){
                district = true;
            }
        }
        if(district){
            list = ycMeasuresMapper.selectYcMeasuresListMore(ycMeasures_v2);
            List<YcMeasures> list1 = ycMeasuresMapper.selectDistrictYcMeasure(ycMeasures_v2);
            for (YcMeasures element:list1){
                if (ycDataMapper.selectYcDataByDataId(element.getDataId()) != null){
                    YcData ycData = ycDataMapper.selectYcDataByDataId(element.getDataId());
                    if(ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId()) != null){
                        YcOrg ycOrg = ycOrgMapper.selectYcOrgByOrgId(ycData.getOrgId());
                        if(ycOrg.getOrgName().equals(sysDept.getDeptName())){
                            list.add(element);
                        }
                    }
                }
            }
        }else{
            list = ycMeasuresMapper.selectYcMeasuresListMore(ycMeasures_v2);
        }
        return list;
    }

    /**
     * 新增油厂措施管理
     *
     * @param ycMeasures 油厂措施管理
     * @return 结果
     */
    @Override
    public int insertYcMeasures(YcMeasures ycMeasures) {
        ycMeasures.setCreateTime(DateUtils.getNowDate());
        return ycMeasuresMapper.insertYcMeasures(ycMeasures);
    }

    /**
     * 修改油厂措施管理
     *
     * @param ycMeasures 油厂措施管理
     * @return 结果
     */
    @Override
    public int updateYcMeasures(YcMeasures ycMeasures) {
        return ycMeasuresMapper.updateYcMeasures(ycMeasures);
    }

    /**
     * 批量修改油厂措施管理
     *
     * @param meaIds 油厂措施管理
     * @return 结果
     */
    public int updateYcMeasuresByMeaIds(Long[] meaIds) {
        return ycMeasuresMapper.updateYcMeasuresByMeaIds(meaIds);
    }

    /**
     * 批量删除油厂措施管理
     *
     * @param meaIds 需要删除的油厂措施管理主键
     * @return 结果
     */
    @Override
    public int deleteYcMeasuresByMeaIds(Long[] meaIds) {
        return ycMeasuresMapper.deleteYcMeasuresByMeaIds(meaIds);
    }

    /**
     * 删除油厂措施管理信息
     *
     * @param meaId 油厂措施管理主键
     * @return 结果
     */
    @Override
    public int deleteYcMeasuresByMeaId(Long meaId) {
        return ycMeasuresMapper.deleteYcMeasuresByMeaId(meaId);
    }

    /**
     * 导入措施
     *
     * @param measuresList    指标列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    public String importData(List<YcMeasures> measuresList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(measuresList) || measuresList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (YcMeasures ycMeasures : measuresList) {
            try {
                YcMeasures i = ycMeasuresMapper.selectYcMeasuresByMeaId(ycMeasures.getMeaId());
                if (StringUtils.isNull(i)) {
                    //BeanValidators.validateWithException(validator, ycMeasures);
                    this.insertYcMeasures(ycMeasures);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");
                } else if (isUpdateSupport) {
                    //BeanValidators.validateWithException(validator, ycMeasures);
                    this.updateYcMeasures(ycMeasures);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "导入失败";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
